package com.api.interceptor;

import com.common.context.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * @author 路亚
 * @version 0.1
 * @className FeignRelayUserInterceptor
 * @description
 * @date 2024/12/12 21:04
 * @since jdk11
 */

public class FeignRelayUserInterceptor implements RequestInterceptor {

    /**
     * 使用 openFeign 调用远程接口时，会自动调用此方法，在请求头中设置 userId
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        Long userId = UserContext.getCurrentId();
        if (userId == null) {
            return;
        }
        template.header("user_id", userId.toString());
    }
}
